﻿using System;
using System.Windows.Forms;
using System.Collections.Generic;
using BotonJXC.Entity;
using BotonJXC.BLL;
using BotonJXC.Util;
using BotonJXC.Forms.Events;

namespace BotonJXC.Forms.Controls
{
    /// <summary>
    /// 货品类别树形控件
    /// </summary>
    public partial class ProductTypeControl : BaseInfoControl
    {
        /// <summary>
        /// 选择货品类别时发生的事件
        /// </summary>
        public event ProductTypeSelectedEventHandler ProductTypeSelected;
        ProductTypeBLL _productTypeBll = new ProductTypeBLL();
        ProductType _currentProductType = null;
        private bool _showEditPanel = true;
        public ProductTypeControl()
        {
            InitializeComponent();
        }

        /// <summary>
        /// 获取或设置当前选择的货品类别
        /// </summary>
        public ProductType SelectedProductType
        {
            get
            {
                return _currentProductType;
            }
            set
            {
                _currentProductType = value;
            }
        }

        /// <summary>
        /// 获取或设置是否出现编辑面板
        /// </summary>
        public bool ShowEditPanel
        {
            get { return _showEditPanel; }
            set { _showEditPanel = value; }
        }

        private void LoadProductType()
        {
            TreeNode root = ktvProductType.Nodes[0];
            TreeDataNode<ProductType> types = _productTypeBll.FindAllToTree();
            LoadSubType(root, types);
            root.Expand();
            if (this.ShowEditPanel)
            {
                root.ContextMenuStrip = contextMenuStrip1;
                CreateNodeMenuEvent();
            }
        }

        /// <summary>
        /// 载入货品类别节点数据
        /// </summary>
        /// <param name="treeNode"></param>
        /// <param name="typeNodes"></param>
        private void LoadSubType(TreeNode treeNode, TreeDataNode<ProductType> typeNodes)
        {
            foreach (TreeDataNode<ProductType> tp in typeNodes)
            {
                TreeNode node = new TreeNode();
                if (this.ShowEditPanel)
                {
                    node.ContextMenuStrip = contextMenuStrip1;
                }
                node.Text = tp.Data.TypeName;
                node.Tag = tp.Data;
                treeNode.Nodes.Add(node);
                LoadSubType(node, tp);
            }
        }

        /// <summary>
        /// 创建节点菜单事件
        /// </summary>
        private void CreateNodeMenuEvent() {
            tsmiAddNode.Click += new EventHandler(NodeMenu_Click);
            tsmiDeleteNode.Click += new EventHandler(NodeMenu_Click);
        }

        void NodeMenu_Click(object sender, EventArgs e)
        {
            TreeNode menuNode = ktvProductType.SelectedNode;
            if (menuNode != null)
            {
                ToolStripMenuItem menu = sender as ToolStripMenuItem;

                if (menu == tsmiAddNode)
                {
                    AddInfo();
                }
                else if (menu == tsmiDeleteNode)
                {
                    DeleteInfo();
                }
            }
        }

        private void ProductTypeTreeControl_Load(object sender, EventArgs e)
        {
            if (!this.DesignMode)
            {
                LoadProductType();
            }

            if (!this.ShowEditPanel)
            {
                kryptonGroupBox1.Visible = false;
                kryptonGroupBox2.Dock = DockStyle.Fill;
            }
        }
        private void ktvProductType_AfterSelect(object sender, TreeViewEventArgs e)
        {
            if (ktvProductType.SelectedNode != null)
            {
                this.SelectedProductType = e.Node.Tag as ProductType;
                if (this.ShowEditPanel && this.SelectedProductType != null)
                {
                    ktbTypeName.Text = this.SelectedProductType.TypeName;
                    ktbTypeName.Tag = e.Node;
                }
                if (ProductTypeSelected != null)
                    ProductTypeSelected(this, new ProductTypeSelectedEventArgs(this.SelectedProductType));
            }
        }

        public override IList<Object> SelectedItems
        {
            get
            {
                if (ktvProductType.SelectedNode != null)
                {
                    this.SelectedProductType = ktvProductType.SelectedNode.Tag as ProductType;

                    IList<Object> selectedItems = new List<Object>();
                    selectedItems.Add(this.SelectedProductType);
                    return selectedItems;
                }
                else
                {
                    return null;
                }
            }
        }

        private void ktvProductType_NodeMouseClick(object sender, TreeNodeMouseClickEventArgs e)
        {
            if (e.Button == System.Windows.Forms.MouseButtons.Right)
            {
                ktvProductType.SelectedNode = e.Node;
                e.Node.Checked = true;
            }
        }

        private void ktbTypeName_Leave(object sender, EventArgs e)
        {
            UpdateNodeType();
        }

        private void UpdateNodeType()
        {
            TreeNode node = ktbTypeName.Tag as TreeNode;
            if (node != null)
            {
                node.Text = ktbTypeName.Text.Trim();
                ProductType type = node.Tag as ProductType;
                type.TypeName = node.Text;
            }
        }

        public override void SaveInfo()
        {

        }
        public override void DeleteInfo()
        {

        }
        public override void AddInfo()
        {
            TreeNode node = ktvProductType.SelectedNode;
            TreeNode newNode = new TreeNode();
            ProductType newType = new ProductType();
            newNode.Text = newType.TypeName = "新分类";
            node.Nodes.Add(newNode);
            newNode.Tag = newType;
            ktvProductType.SelectedNode = newNode;
        }

        private void ktbTypeName_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (e.KeyChar == (char)Keys.Enter)
            {
                UpdateNodeType();
            }
        }
    }
}
